package technique.多数元素;

public class Solution {
    public int majorityElement(int[] nums) {
        int ans = nums[0];
        int count = 1;
        for (int i = 1; i < nums.length; i++) {
            if (ans == nums[i]) {
                count++;
            } else {
                count--;
                if (count == 0) {
                    ans = nums[++i];
                    count = 1;
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.majorityElement(new int[]{2, 2, 1, 1, 1, 2, 2}));
    }
}
